//var app = angular.module('app', []);
app
		.controller(
				'DRCtr',
				function($scope, $http, $filter, $state, filterFilter,$rootScope) {

					
					
					 $scope.statuses = ["All","New","Hold","Open","Cancel","Closed"];
					$http
							.get(
									"/DRMS/api/demandrequest/getAllDrmsDemandRequest")
							.then(
									function(response) {
										// $scope.DRs = response.data;
										$scope.data = response.data;
										
										
									//	console.log($scope.data);
									
										$scope
												.$watch(
														'DRSearch+demandRequest.status',
														function(newVal, oldVal) {

															// $scope.DRs =
															// $filter('DRSearch')($scope.data);
															var DRsFilter = [];	
															$scope.DRs = filterFilter(
																	$scope.data,
																	$scope.DRSearch);
											
															console.log($scope.demandRequest.status);
																if($scope.demandRequest.status =="New"){
																	for(var i = 0; i<$scope.data.length;i++){
																		if($scope.data[i].status=='New'){
																			DRsFilter.push($scope.data[i]);
																		}
																	}
																	
																	$scope.DRs =DRsFilter;
																	$scope.DRs = filterFilter(
																			$scope.DRs	,
																			$scope.DRSearch);
																}
																if($scope.demandRequest.status =="Hold"){
																	for(var i = 0; i<$scope.data.length;i++){
																		if($scope.data[i].status=='Hold'){
																			DRsFilter.push($scope.data[i]);
																		}
																	}
																	$scope.DRs =DRsFilter;
																	$scope.DRs = filterFilter(
																			$scope.DRs	,
																			$scope.DRSearch);
																}
																if($scope.demandRequest.status =="Open"){
																	for(var i = 0; i<$scope.data.length;i++){
																		if($scope.data[i].status=='Open'){
																			DRsFilter.push($scope.data[i]);
																		}
																	}
																	$scope.DRs =DRsFilter;
																	$scope.DRs = filterFilter(
																			$scope.DRs	,
																			$scope.DRSearch);
																}
																if($scope.demandRequest.status =="Cancel"){
																	for(var i = 0; i<$scope.data.length;i++){
																		if($scope.data[i].status=='Cancel'){
																			DRsFilter.push($scope.data[i]);
																		}
																	}
																	$scope.DRs =DRsFilter;
																	$scope.DRs = filterFilter(
																			$scope.DRs	,
																			$scope.DRSearch);
																}
																
																if($scope.demandRequest.status =="Closed"){
																	for(var i = 0; i<$scope.data.length;i++){
																		if($scope.data[i].status=='Closed'){
																			DRsFilter.push($scope.data[i]);
																		}
																	}
																	$scope.DRs =DRsFilter;
																	$scope.DRs = filterFilter(
																			$scope.DRs	,
																			$scope.DRSearch);
																}
															
															
															$scope.pageSize = 10;
															$scope.pages = Math
																	.ceil($scope.DRs.length
																			/ $scope.pageSize);
															$scope.newPages = $scope.pages > 5 ? 5
																	: $scope.pages;
															$scope.pageList = [];
															$scope.selPage = 1;
															$scope.setData = function() {
																$scope.items = $scope.DRs
																		.slice(
																				($scope.pageSize * ($scope.selPage - 1)),
																				($scope.selPage * $scope.pageSize));
															}
															$scope.items = $scope.DRs
																	.slice(
																			0,
																			$scope.pageSize);
															for (var i = 0; i < $scope.newPages; i++) {
																$scope.pageList
																		.push(i + 1);
															}
															$scope.selectPage = function(
																	page) {
																
																if(page==1){
																	var newPageList = [];
																	if($scope.pages>5){
																		for(var i = 1;i<=5; i++){
																			  newPageList.push(i);
																		}
																	}
																	else{
																		for(var i = 1;i<=$scope.pages; i++){
																		  newPageList.push(i);
																		}
																	}
																	
																	$scope.pageList = newPageList;
																}
																
																
																if (page < 1
																		|| page > $scope.pages)
																	return;

																if (page > 2) {
																	var newpageList = [];
																	for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages
																			: (page + 2)); i++) {
																		newpageList
																				.push(i + 1);
																	}
																	$scope.pageList = newpageList;
																}
																$scope.selPage = page;
																$scope
																		.setData();
																$scope
																		.isActivePage(page);
															};

															$scope.isActivePage = function(
																	page) {
																return $scope.selPage == page;
															};
															$scope.Previous = function() {
																$scope
																		.selectPage($scope.selPage - 1);
															}
															$scope.Next = function() {
																$scope
																		.selectPage($scope.selPage + 1);
															};

														});

										})
	
									$scope.excelExport =function(){
								
									$http({
										method :'POST',
										url: '/DRMS/api/demandrequest/export',
										headers: {
											'Content-Type': 'application/json'
										},
										data :$scope.DRs,
										responseType: 'arraybuffer'
									}).success(function (data) {
									    
										var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
								   
										var objectUrl = URL.createObjectURL(blob);
								        var a = document.createElement('a');
								        document.body.appendChild(a);
								        a.setAttribute('style', 'display:none');
								        a.setAttribute('href', objectUrl);
								        a.setAttribute('download', "DemandRequest.xls");
								        a.click();
								        URL.revokeObjectURL(objectUrl);
								    });
							
						}
										
									 $scope.option = function(demandRequest, event){
										
											$rootScope.demandRequest = demandRequest;
										
											$state.go('recrut2.dmdreq.option');
											event.stopPropagation();
											event.preventDefault();
					  				}
									$scope.addPR = function(demandRequest,event){
										$rootScope.demandRequest = demandRequest;
										
										$state.go('recrut2.dmdreq.addPro');
										event.stopPropagation();
										event.preventDefault();
									}
									
									
									
									
				});






